package cn.edu.ujn.park.controller;

import cn.edu.ujn.park.dao.ParkingSpace;
import cn.edu.ujn.park.service.IParkingSpace;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class ParkingSpaceController {
    @Autowired
    private IParkingSpace parkingSpace;

    @GetMapping("/parking_space_manage")
    public String list(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, ParkingSpace space){
        space.setStart((page-1)*rows);
        space.setRows(rows);
        Page<ParkingSpace> page1 =parkingSpace.selectAll(space);
        model.addAttribute("page",page1);
        return "parking_space_manage";

    }
    @GetMapping("/parking_space_manage_select")
    public String select(Model model,
                        @RequestParam("spaceNumber") String spaceNumber){
        Page<ParkingSpace> page1 =parkingSpace.selectByNumber(spaceNumber);
        model.addAttribute("page",page1);
        return "parking_space_manage";

    }
    @PostMapping("/space")
    @ResponseBody
    public String creat(ParkingSpace space){
        int insert = parkingSpace.insert(space);
        return "OK";
    }

    @GetMapping("/space/{id}")
    @ResponseBody
    public ParkingSpace editUser(@PathVariable("id") Integer id){
        return parkingSpace.selectByPrimaryKey(id);
    }

    @PutMapping("/space")
    @ResponseBody
    public String updateUser(@RequestBody ParkingSpace space){
        System.out.println(space);
        parkingSpace.updateByPrimaryKey(space);
        return "OK";
    }

    @DeleteMapping("/space/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") Integer id){
        int insert = parkingSpace.deleteByPrimaryKey(id);
        return "OK";
    }

}
